//===============================================================================
// Microsoft patterns & practices
// Mobile Client Software Factory - July 2006
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Net;
using Microsoft.Practices.TestUtilities;

namespace Microsoft.Practices.Mobile.EndpointCatalog.Tests
{
	[TestClass]
	public class EndpointFixture
	{

        [TestMethod]
        public void GetAccountNameForNetworkReturnsDefaultWhenNetworkNameIsNull()
        {
            Endpoint endpoint = new Endpoint("MyIsp");
            EndpointConfig ec = new EndpointConfig("", "account");
            endpoint.Default = ec;

            string account = endpoint.GetAccountNameForNetwork(null);
            Assert.IsNotNull(account);
            Assert.AreEqual(account, ec.AccountName);
        }

        [TestMethod]
        public void GetAddressForNetworkReturnsDefaultWhenNetworkNameIsNull()
        {
            Endpoint endpoint = new Endpoint("MyIsp");
            EndpointConfig ec = new EndpointConfig("", "account");
            endpoint.Default = ec;

            string address = endpoint.GetAddressForNetwork(null);
            Assert.IsNotNull(address);
            Assert.AreEqual(ec.Address, address);
        }

        [TestMethod]
        public void GetAccountNameForNetworkReturnsDefaultWhenNetworkNameIsEmpty()
        {
            Endpoint endpoint = new Endpoint("MyIsp");
            EndpointConfig ec = new EndpointConfig("", "account");
            endpoint.Default = ec;

            string account = endpoint.GetAccountNameForNetwork(String.Empty);
            Assert.IsNotNull(account);
            Assert.AreEqual(account, ec.AccountName);
        }

        [TestMethod]
        public void GetAddressForNetworkReturnsDefaultWhenNetworkNameIsEmpty()
        {
            Endpoint endpoint = new Endpoint("MyIsp");
            EndpointConfig ec = new EndpointConfig("", "account");
            endpoint.Default = ec;

            string address = endpoint.GetAddressForNetwork(String.Empty);
            Assert.IsNotNull(address);
            Assert.AreEqual(ec.Address, address);
        }

        [TestMethod]
		public void EndpointProvidesCollectionOfRegisteredNetworkNames()
		{
			Endpoint ep = new Endpoint("");
			ep.SetConfiguration("One", new EndpointConfig("URL1"));
			ep.SetConfiguration("Two", new EndpointConfig("URL2"));
			ep.SetConfiguration("Three", new EndpointConfig("URL3"));

			Assert.AreEqual(3, ep.NetworkNames.Count);
			Assert.IsTrue(ep.NetworkNames.Contains("One"));
			Assert.IsTrue(ep.NetworkNames.Contains("Two"));
			Assert.IsTrue(ep.NetworkNames.Contains("Three"));
		}

		[TestMethod]
		public void EmptyEndpointConfigurationNameRegistrationShouldThrowsAnArgumentException()
		{
			Endpoint endpoint = new Endpoint("");
			EndpointConfig ec = new EndpointConfig("");
			ExtendedAssert.Throws<ArgumentException>(
						delegate { endpoint.SetConfiguration("", ec); }); //Should throws an ArgumentException
		}

		[TestMethod]
		public void NullConfigurationNameRegistrationShouldThrowsAnArgumentException()
		{
			Endpoint endpoint = new Endpoint("");
			EndpointConfig ec = new EndpointConfig("");
			ExtendedAssert.Throws<ArgumentException>(
						delegate { endpoint.SetConfiguration(null, ec); });//Should throws an ArgumentException
		}

		[TestMethod]
		public void NullEndpointConfigurationRegistrationShouldThrowsAnArgumentNullException()
		{
			Endpoint endpoint = new Endpoint("");
			ExtendedAssert.Throws<ArgumentNullException>(
						delegate { endpoint.SetConfiguration("A", null); });
		}

		[TestMethod]
		public void EmptyAddressInEndpointConfigurationRegistrationShouldThrowsAnArgumentException()
		{
			Endpoint endpoint = new Endpoint("");
			EndpointConfig ec = new EndpointConfig("");

			ExtendedAssert.Throws<ArgumentException>(
						delegate { endpoint.SetConfiguration("Network", ec); });
		}

		[TestMethod]
		public void ModifyingEndpointConfigReplacePreviousConfig()
		{
			Endpoint ep = new Endpoint("Endpoint");
			EndpointConfig ec1 = new EndpointConfig("URL1");

			ep.SetConfiguration("Network1", ec1);

			EndpointConfig ec2 = new EndpointConfig("URL2");
			ep.SetConfiguration("Network1", ec2);

			Assert.AreEqual(ec2.Address, ep.GetAddressForNetwork("Network1"));
			Assert.AreEqual(ec2.AccountName, ep.GetAccountNameForNetwork("Network1"));
			Assert.AreEqual(1, ep.NetworkNames.Count);
		}

		[TestMethod]
		public void SetNullConfigurationThrowsArgumentNullException()
		{
			Endpoint endpoint = new Endpoint("Endpoint");
			ExtendedAssert.Throws<ArgumentNullException>(
						delegate { endpoint.SetConfiguration("Network", null); });
		}

		[TestMethod]
		public void ModifyDefaultAccountNameReplacesPreviousDefaultAccountName()
		{
			Endpoint ep = new Endpoint("Endpoint");
			EndpointConfig ec = new EndpointConfig("Address", "MainAccount");
			string expectedCreds = "AlternativeAccount";
			ep.Default = ec;
			ep.Default.AccountName = expectedCreds;
			Assert.AreEqual(expectedCreds, ep.GetAccountNameForNetwork(null));
		}

	}
}
